package com.study.java.utils;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

/**
* @Name: WebUtils
* @Description: 页面表单参数封装工具类
* @Author: XXX
* @CreateDate: XXX
* @Version: V1.0
 */
public class WebUtils {
	
	/**
	* @Name: fillBean
	* @Description: 将页面请求参数封装到表单Bean对象中
	* @Author: XXX
	* @Version: V1.0
	* @CreateDate: XXX
	* @Parameters: @param request
	* @Parameters: @param clazz
	* @Return: T
	 */
	public static <T> T fillBean(HttpServletRequest request, Class<T> clazz) {
		T bean = null ;
		try {
			bean = clazz.newInstance() ;
			//设置请求参数内容编码，适用于post提交
			request.setCharacterEncoding("UTF-8") ;
			//将页面提交表单中的参数注入到bean实例属性中
			BeanUtils.populate(bean, request.getParameterMap()) ;
		} catch (Exception e) {
			throw new RuntimeException(e) ;
		}
		return bean ;
	}
	
	/**
	* @Name: copyProperties
	* @Description: 对象拷贝
	* @Author: XXX
	* @Version: V1.0
	* @CreateDate: XXX
	* @Parameters: @param tClass 目标字节码文件对象
	* @Parameters: @param oClass 源字节码文件对象
	* @Return: T 返回目标类实例化对象
	 */
	public static <T> T copyProperties(Class<T> tClass, Object oBean) {
		T tBean = null ;
		try {
			tBean = tClass.newInstance() ;
			//注册日期类型转换器
			ConvertUtils.register(new DateLocaleConverter(), Date.class) ;
			//对象拷贝
			BeanUtils.copyProperties(tBean, oBean) ;
		} catch (Exception e) {
			throw new RuntimeException(e) ;
		}
		return tBean ;
	}
	

}







